package cn.itcast.web;


import cn.itcast.pojo.earnings;
import cn.itcast.pojo.info;
import cn.itcast.pojo.user;
import cn.itcast.service.impl.InfoServiceImpl;
import cn.itcast.service.impl.UserServiceImpl;
import cn.itcast.util.OutputStringController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping(value="/wxshop/info",produces="text/html;charset=UTF-8")
public class InfoController {

    @Resource
    InfoServiceImpl infoService;
    @Resource
    OutputStringController osc;



    /**
     *
     * @Description:
     * 获取所有消息
     * @auther: 欧阳辉
     * @date: 14:51 2020/3/17
     * @param: [map]
     * @return: java.lang.String
     *
     */
    @PostMapping("/findAllInfo")
    public String findAllInfo(@RequestBody Map<String,String> map){
        log.debug("获取所有消息");
        String openId = map.get("openId");

        //返回结果
        String results = "";
        List<info> infos = infoService.findAllInfo(openId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        for(int i = 0 ;i<infos.size() ;i++){
            if(StringUtils.isNotBlank(ObjectUtils.toString(infos.get(i).getInfoTime()))){
                infos.get(i).setInfoTimeString(sdf.format(infos.get(i).getInfoTime()));
            }
        }
        if(!infos.isEmpty()){
            results = osc.resultSuccess("yes",infos);
            log.debug("获取所有消息成功");
        }else{
            results = osc.failure("no");
            log.debug("获取所有消息失败");
        }

        return results;
    }


    /**
     *
     * @Description:
     * 获取分类消息
     * @auther: 欧阳辉
     * @date: 14:51 2020/3/17
     * @param: [map]
     * @return: java.lang.String
     *
     */
    @PostMapping("/findTypeInfo")
    public String findTypeInfo(@RequestBody Map<String,String> map){
        log.debug("获取所有消息");
        String openId = map.get("openId");
        String type = map.get("type");

        //返回结果
        String results = "";
        List<info> infos = infoService.findTypeInfo(openId,type);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        for(int i = 0 ;i<infos.size() ;i++){
            if(StringUtils.isNotBlank(ObjectUtils.toString(infos.get(i).getInfoTime()))){
                infos.get(i).setInfoTimeString(sdf.format(infos.get(i).getInfoTime()));
            }
        }
        if(!infos.isEmpty()){
            results = osc.resultSuccess("yes",infos);
            log.debug("获取所有消息成功");
        }else{
            results = osc.failure("no");
            log.debug("获取所有消息失败");
        }

        return results;
    }

    /**
     *
     * @Description:
     * 获取未读消息数
     * @auther: 欧阳辉
     * @date: 14:51 2020/3/17
     * @param: [map]
     * @return: java.lang.String
     *
     */
    @PostMapping("/findNumRead")
    public String findNumRead(@RequestBody Map<String,String> map){
        log.debug("获取未读消息数");
        String openId = map.get("openId");

        //返回结果
        String results = "";
        int num = infoService.findNumRead(openId);
        if(num!=0){
            results = osc.resultSuccess("yes",num);
            log.debug("获取未读消息数成功");
        }else{
            results = osc.failure("no");
            log.debug("获取未读消息数失败");
        }

        return results;
    }

    /**
     *
     * @Description:
     * 已读信息
     * @auther: 欧阳辉
     * @date: 14:51 2020/3/17
     * @param: [map]
     * @return: java.lang.String
     *
     */
    @PostMapping("/overRead")
    public String overRead(@RequestBody Map<String,String> map){
        log.debug("已读信息");
        String id = map.get("id");

        //返回结果
        String results = "";
        boolean isread = infoService.overRead(id);
        if(isread){
            results = osc.success("yes");
            log.debug("已读信息成功");
        }else{
            results = osc.failure("no");
            log.debug("已读信息失败");
        }

        return results;
    }


    @PostMapping("/updateMessage")
    public String readMessage(@RequestBody Map<String,String> map){
        String openId = map.get("openId");

        //返回结果
        String results = "";
        if (infoService.readMessage(openId)){
            results = osc.success("yes");
            log.debug("一键已读信息成功");
        }else{
            results = osc.failure("no");
            log.debug("一键已读信息失败");
        }
        return results;
    }


}
